{% extends "layout/control.html" %}

{% block title %}图床管理员{% endblock %}

{% block head %}
<style>
    .layui-tab-content .layui-elem-field {
        font-size: 12px;
    }
    .layui-tab-content .layui-elem-field legend {
        font-size: 14px;
        font-weight: bold;
        color: #009688;
    }
    .layui-tab-content .layui-elem-field .layui-elem-field legend {
        font-size: 10px;
    }
    .layui-inline, img {
        display: inline;
    }
    .upload-textarea {
        min-height: 60px;
    }
    .my-textarea {
        min-height: 80px;
    }
</style>
{% endblock %}

{% block content %}
<div class="layui-container">
    <div class="layui-tab layui-tab-brief" lay-filter="tab">
        <ul class="layui-tab-title">
            <li class="layui-this" lay-id="setting"><i class="saintic-icon saintic-icon-system-setting"></i> 配置管理</li>
            <li lay-id="hook"><i class="saintic-icon saintic-icon-plugin"></i> 钩子扩展</li>
            <li lay-id="user"><i class="saintic-icon saintic-icon-user-manager"></i> 用户管理</li>
            <li lay-id="picbed"><i class="saintic-icon saintic-icon-jump"></i> 全站图片管理</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form" id="control" lay-filter="setting" autocomplete="off">

                    <div class="layui-row">
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                            <fieldset class="layui-elem-field layui-field-title">
                                <legend>站点</legend>
                                <div class="layui-field-box">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">站点名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="title_name" value="{{ g.site.title_name }}"
                                                placeholder="站点名称，统一标题后缀，默认sapic" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">站点关键词</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="seo_keywords" value="{{ g.site.seo_keywords }}"
                                                placeholder="指定站点的SEO关键词" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">站点描述</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="seo_description"
                                                value="{{ g.site.seo_description }}" placeholder="指定站点的SEO内容描述"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">站点图标</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="favicon" value="{{ g.site.favicon }}"
                                                placeholder="站点图标地址，默认是：{{ url_for('static', filename='img/favicon.png') }}"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">Logo</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="logo" value="{{ g.site.logo }}"
                                                placeholder="站点Logo地址，默认是：{{ url_for('static', filename='img/logo.png') }}"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">登录欢迎文案</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="login_title" value="{{ g.site.login_title }}"
                                                placeholder="登录页面的标题文字，默认是：登录你的Sapic" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">背景图</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="bg_mg" value="{{ g.site.bg_mg }}"
                                                placeholder="站点PC端背景图地址，下拉可选择默认提供的背景图" autocomplete="off"
                                                class="layui-input" list="bg_mg_defaults">
                                            <datalist id="bg_mg_defaults">
                                                <option value="https://open.saintic.com/api/bingPic">
                                                <option value="{{ url_for('static', filename='img/bg1.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/bg2.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/bg3.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/bg4.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/bg5.jpg') }}">
                                            </datalist>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">移动端背景图</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="bg_mobile" value="{{ g.site.bg_mobile }}"
                                                placeholder="站点移动端背景图地址，下拉可选择默认提供的背景图" autocomplete="off"
                                                class="layui-input" list="bg_mobile_defaults">
                                            <datalist id="bg_mobile_defaults">
                                                <option value="{{ url_for('static', filename='img/mbg1.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/mbg2.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/mbg3.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/mbg4.jpg') }}">
                                                <option value="{{ url_for('static', filename='img/mbg5.jpg') }}">
                                            </datalist>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">背景透明度</label>
                                        <div class="layui-input-block">
                                            <input type="number" step=0.1 min=0 max=1 name="bg_ransparency"
                                                value="{{ g.site.bg_ransparency }}"
                                                placeholder="站点背景透明度，0-1之间（值越大越透明），默认是0.5" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">备案号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="beian" value="{{ g.site.beian }}"
                                                placeholder="ICP备案号，若有公网安备请加半角逗号再填写公安备案号" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">页脚</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="footer" value="{{ g.site.footer }}"
                                                placeholder="位于Copyright年份与版权声明之间" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label"><i class="saintic-icon saintic-icon-bulletin"
                                                id="tip-bulletin"></i> 全站公告</label>
                                        <div class="layui-input-block">
                                            <textarea name="bulletin" placeholder="页面顶部公告栏" value=""
                                                class="layui-textarea my-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label">关于本站</label>
                                        <div class="layui-input-block">
                                            <textarea name="about" placeholder="本站的额外说明，通过页脚About展示" value=""
                                                class="layui-textarea my-textarea"></textarea>
                                        </div>
                                    </div>
                                    {{ intpl("sitesetting") }}
                                </div>
                            </fieldset>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                            <fieldset class="layui-elem-field layui-field-title">
                                <legend>上传</legend>
                                <div class="layui-field-box">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">匿名上传</label>
                                        <div class="layui-input-block">
                                            <input type="checkbox" name="anonymous" lay-skin="switch" lay-text="ON|OFF"
                                                {% if is_true(g.site.anonymous) %}checked="checked" {% endif %}
                                                autocomplete="off" value="1">
                                        </div>
                                    </div>
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label"><i class="saintic-icon saintic-icon-tip"
                                                id="tip-uploadbeforehtml"></i> 首页上传区域提示</label>
                                        <div class="layui-input-block">
                                            <textarea name="upload_beforehtml" placeholder="首页上传区域上方文字提示" value=""
                                                class="layui-textarea upload-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">图片后缀限制</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="upload_exts" value="{{ g.site.upload_exts }}"
                                                placeholder="竖线分隔上传允许的后缀，默认是jpg|jpeg|png|gif|webp|bmp"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label" title="{{ g.site.upload_size or 20 }}MB">大小限制</label>
                                        <div class="layui-input-block">
                                            <input type="number" step=1 name="upload_size" max="{{ config.MAX_UPLOAD }}" min="1"
                                                value="{{ g.site.upload_size }}" placeholder="上传允许的大小，单位MB，默认是20"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">数量限制</label>
                                        <div class="layui-input-block">
                                            <input type="number" step=1 min=0 max=10 name="upload_number"
                                                value="{{ g.site.upload_number }}" placeholder="首页上传允许的数量，最大10，默认是10"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label" style="color:red"><i
                                                class="saintic-icon saintic-icon-warn"></i> 上传字段</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="upload_field" value="{{ g.site.upload_field }}"
                                                placeholder="上传接口获取文件字段名，默认picbed，谨慎修改！" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    {% set bes = get_call_list("upimg_save", _type="func") %}
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户分组上传</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="upload_group" value="{{ g.site.upload_group }}"
                                                placeholder="按用户标签设置上传后端，格式是label:sender，半角逗号分隔" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">分组上传限制</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="upload_limit" value="{{ g.site.upload_limit }}"
                                                placeholder="按标签限制上传数量，格式是label:number，半角逗号分隔" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label" id="tip-be"><i
                                                class="saintic-icon saintic-icon-storage-cloud"></i> 默认存储在</label>
                                        <div class="layui-input-block">
                                            <select name="upload_includes" lay-verify="required">
                                                {% for h in bes %}
                                                <option value="{{ h.name }}"
                                                    {% if h.name in g.site.upload_includes %}selected="selected"
                                                    {% endif %}>{{ h.name }}{% if h.description %}（{{ h.description }}）{% endif %}
                                                </option>
                                                {% endfor %}
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">图片路径前缀规则: user/&lt;&gt;</label>
                                        <div class="layui-input-block">
                                            <input type="radio" name="upload_path_rule" value="default" title="默认(无路径)"
                                                {% if g.site.upload_path_rule|d("default") == "default" %}checked="checked"
                                                {% endif %} autocomplete="off">
                                            <input type="radio" name="upload_path_rule" value="date1" title="年/月/日"
                                                {% if g.site.upload_path_rule == "date1" %}checked="checked" {% endif %}
                                                autocomplete="off">
                                            <input type="radio" name="upload_path_rule" value="date2" title="年月日"
                                                {% if g.site.upload_path_rule == "date2" %}checked="checked" {% endif %}
                                                autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">图片文件命名规则: /&lt;name&gt;</label>
                                        <div class="layui-input-block">
                                            <input type="radio" name="upload_file_rule" value="default" title="默认(原名)"
                                                {% if g.site.upload_file_rule|d("default") == "default" %}checked="checked"
                                                {% endif %} autocomplete="off">
                                            <input type="radio" name="upload_file_rule" value="time1" title="时间戳(毫秒)"
                                                {% if g.site.upload_file_rule == "time1" %}checked="checked" {% endif %}
                                                autocomplete="off">
                                            <input type="radio" name="upload_file_rule" value="time2" title="时间戳+随机数"
                                                {% if g.site.upload_file_rule == "time2" %}checked="checked" {% endif %}
                                                autocomplete="off">
                                            <input type="radio" name="upload_file_rule" value="time3" title="年月日时分秒+随机数"
                                                {% if g.site.upload_file_rule == "time3" %}checked="checked" {% endif %}
                                                autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label" style="width: auto;">上述两条规则允许用户覆盖</label>
                                        <div class="layui-input-block" style="width: auto;">
                                            <input type="checkbox" name="upload_rule_overridden" lay-skin="switch"
                                                lay-text="ON|OFF"
                                                {% if is_true(g.site.upload_rule_overridden) %}checked="checked"
                                                {% endif %} autocomplete="off" value="1">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">视频上传</label>
                                        <div class="layui-input-block" style="width: auto;">
                                            <input type="checkbox" name="upload_video" lay-skin="switch"
                                                lay-text="ON|OFF"
                                                {% if is_true(g.site.upload_video) %}checked="checked"
                                                {% endif %} autocomplete="off" value="1">
                                        </div>
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                    </div>
                    <div class="layui-row">
                        <fieldset class="layui-elem-field layui-field-title">
                            <legend>系统</legend>
                            <div class="layui-field-box">
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">CORS Origin</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="cors" value="{{ g.site.cors }}"
                                                placeholder="设置跨域允许的源站(仅协议和域名、端口)，允许*" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><i class="saintic-icon saintic-icon-info"
                                                id="tip-register-options"></i> 注册选项</label>
                                        <div class="layui-input-block">
                                            <input type="checkbox" name="register" title="开放注册" lay-skin="primary"
                                                {% if is_true(g.site.register) %}checked="checked" {% endif %}
                                                autocomplete="off" value="1">
                                            <input type="checkbox" name="review" title="注册审核" lay-skin="primary"
                                                {% if is_true(g.site.review) %}checked="checked" {% endif %}
                                                autocomplete="off" value="1">
                                            <input type="checkbox" name="disable_login" title="禁止登录" lay-skin="primary"
                                                {% if is_true(g.site.disable_login) %}checked="checked" {% endif %}
                                                autocomplete="off" value="1">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">禁止注册用户</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="forbidden_username"
                                                value="{{ g.site.forbidden_username }}"
                                                placeholder="禁止注册的用户名，半角逗号分割多个，默认包含anonymous" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">注册默认分组</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="default_userlabel"
                                                value="{{ g.site.default_userlabel }}"
                                                placeholder="新注册用户默认标签（用户初始分组）" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">注册审核邮箱</label>
                                        <div class="layui-input-block">
                                            <input type="email" name="review_email"
                                                value="{{ g.site.review_email }}"
                                                placeholder="开启注册审核后，发送邮件提醒" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><i class="saintic-icon saintic-icon-info" id="tip-proxy"></i>代理服务</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="proxies"
                                                value="{{ g.site.proxies }}"
                                                placeholder="全局外部HTTP请求代理" autocomplete="off"
                                                class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
                            <fieldset class="layui-elem-field layui-field-title">
                                <legend>钩子</legend>
                                <div class="layui-field-box">
                                    <div class="layui-row layui-col-space10">
                                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                            <div class="layui-form-item">
                                                <label class="layui-form-label" id="tip-sa"><b style="color: red;">?</b>
                                                    第三方认证</label>
                                                <div class="layui-input-block">
                                                    <select name="site_auth">
                                                        <option value="">默认（取消选择）</option>
                                                        {% for h in get_call_list("site_auth", _type="bool") %}
                                                        <option value="{{ h.name }}"
                                                            {% if h.name == g.site.site_auth %}selected="selected"
                                                            {% endif %}>{{ h.name }}{% if h.description %}（{{ h.description }}）{% endif %}
                                                        </option>
                                                        {% endfor %}
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        {{ intpl("hooksetting") }}
                                    </div>
                                    <div class="layui-row layui-col-space10">
                                        {{ intpl("localhooksetting") }}
                                    </div>
                                    <div class="layui-row layui-col-space10">
                                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md6">
                                            <fieldset class="layui-elem-field">
                                                <legend style="font-size: 13px;">邮件服务设置 <sub><a href="javascript:;"
                                                        style="font-size:10px" id="SendTestMail">{ 发送测试邮件 }</a></sub>
                                                </legend>
                                                <div class="layui-field-box">
                                                    <div class="layui-form-item">
                                                            <label class="layui-form-label" >发件人名称</label>
                                                            <div class="layui-input-block">
                                                                <input type="text" name="email_from_name"
                                                                    value="{{ g.site.email_from_name }}"
                                                                    placeholder="统一发件人别名，默认站点名" autocomplete="off"
                                                                    class="layui-input">
                                                            </div>
                                                    </div>
                                                    {{ intpl("emailsetting") }}
                                                </div>
                                            </fieldset>
                                        </div>
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="*" id="saveSetting">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <table id="hook" lay-filter="hook" class="layui-table"></table>
            </div>
            <div class="layui-tab-item">
                <table id="usermanager" lay-filter="usermanager" class="layui-table"></table>
            </div>
            <div class="layui-tab-item"></div>
        </div>
    </div>
</div>
{% endblock %}

{% block script %}
{% raw %}
<script type="text/html" id="hook_action">
    {{#  if(d.state === 'enabled'){ }}
    <a href="javascript:;" style="color:#FFB800" lay-event="disable" title="禁用"><i class="saintic-icon saintic-icon-ban-circle"></i></a>
    {{# } else { }}
    <a href="javascript:;" style="color:#1E9FFF" lay-event="enable" title="启用"><i class="saintic-icon saintic-icon-success"></i></a>
    {{#  } }}
    &nbsp;
    {{#  if(d.family !== 'local'){ }}
    <a href="javascript:;" style="color:#FF5722" lay-event="remove" title="删除"><i class="saintic-icon saintic-icon-trash"></i></a>
    {{#  } }}
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="reloadHookManager">重载钩子管理器</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="installPackage">安装第三方包</button>
        <button class="layui-btn layui-btn-sm" lay-event="addHookName">添加第三方钩子</button>
    </div>
</script>
<script type="text/html" id="usermanager_action">
    {{#  if(d.status === -1){ }}
    <a href="javascript:;" style="color:#009688" lay-event="review" title="审核用户"><i class="saintic-icon saintic-icon-auditing-1"></i></a>
    &nbsp;&nbsp;
    {{#  } }}
    {{#  if(d.status !== 0){ }}
    <a href="javascript:;" style="color:#FFB800" lay-event="disable" title="禁用用户"><i class="saintic-icon saintic-icon-user-disable"></i></a>
    {{#  } }}
    {{#  if(d.status === 0){ }}
    <a href="javascript:;" style="color:#1E9FFF" lay-event="enable" title="启用用户"><i class="saintic-icon saintic-icon-user-enable"></i></a>
    {{#  } }}
    &nbsp;&nbsp;
    <a href="javascript:;" style="color:#FF5722" lay-event="remove" title="删除用户"><i class="saintic-icon saintic-icon-user-delete"></i></a>
</script>
<script type="text/html" id="is_admin_tpl">
    <input type="checkbox" data-username="{{ d.username }}" value="1" lay-skin="primary" lay-filter="set_admin"
    {{ d.is_admin === true ? 'checked' : '' }}>
</script>
<script type="text/html" id="thidhook_action">
    <a href="javascript:;" style="color:#FF5722" lay-event="install" title="安装/升级/使用"><i class="saintic-icon saintic-icon-setup"></i></a>
</script>
<script id="thirhook_link" type="text/html">
    <a href="{{ d.github }}" target="_blank" title="GitHub"><i class="saintic-icon saintic-icon-github"></i></a>&nbsp;
    {{#  if(d.pypi){ }}
    <a href="{{ d.pypi }}" target="_blank" title="Pypi"><i class="saintic-icon saintic-icon-pypi"></i></a>&nbsp;
    {{#  } }}
    {{#  if(d.pkg.installed === true){ }}
    <abbr style="color:#009688" title="本地已安装 {{ d.pkg.local.version }} 版本">{{ d.name }}</abbr>
    {{#  } else { }}
    {{ d.name }}
    {{#  } }}
</script>
<script id="thirdhook_show_tpl" type="text/html">
<div style="padding: 10px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-bottom:0px">
        <legend style="color:#009688;font-size:14px">已审核且开源的第三方钩子列表&nbsp;
            <sub><a href="javascript:;" id="refreshThirdHookTable" style="color:#01AAED;font-size:0.85em">刷新缓存</a></sub></legend>
        <div class="layui-field-box">
            <table id="thid-hook-list" lay-filter="thid-hook-list" class="layui-table"></table>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top:0px">
        <legend style="color:#FFB800;font-size:14px">安装/升级（其他/本地）第三方包</legend>
        <div class="layui-field-box">
            <div class="layui-form" action="">
                <input type="text" name="package" required lay-verify="required" lay-verType="tips" placeholder="The pypi package name or VCS url" autocomplete="off" class="layui-input" style="width:300px;display:inline-block">
                <button type="button" title="提交安装" class="layui-btn layui-btn-xs layui-btn-radius" lay-submit lay-filter="addInstallPackage"><i class="layui-icon layui-icon-ok"></i></button>
            </div>
        </div>
    </fieldset>
</div>
</script>
{% endraw %}
{{ get_page_msg() }}
<script>
    layui.use(['picbed', 'layer', 'form', 'table', 'element', 'util', 'laytpl'], function () {
        var $ = layui.$,
            picbed = layui.picbed,
            util = layui.util,
            form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laytpl = layui.laytpl,
            element = layui.element;
        //显示当前tab
        if (location.hash) {
            var layid = location.hash.replace(/^#/, '');
            element.tabChange('tab', layid);
            // Maybe layui bug
            if (layid === "picbed") {
                location.href = "{{ url_for('front.my', is_mgr=1, _anchor='!picbed') }}";
            }
        }
        //监听tab切换
        element.on('tab(tab)', function (data) {
            var othis = $(this),
                layid = othis.attr('lay-id');
            if (layid) {
                if (layid === "picbed") {
                    location.href = "{{ url_for('front.my', is_mgr=1, _anchor='!picbed') }}";
                }
                location.hash = layid;
            }
        });
        //提示信息
        $('#tip-be').on("mouseover click",function() {
            layer.tips('存储后端取决于扩展的钩子方法：upimg_save',
                this, {tips: 1}
            );
        });
        $('#tip-sa').on("mouseover click",function() {
            layer.tips('注意：如不熟悉请勿开启！<br>第三方认证方式取决于扩展的钩子方法：<br>登录路由：login_handler<br>登出路由：logout_handler<br>登录接口：login_api',
                this, {tips: 1}
            );
        });
        $('#tip-sc').on("mouseover click",function() {
            layer.tips('<a href="https://sendcloud.sohu.com/" target="_blank" style="color:red">SendCloud</a>是搜狐旗下云端邮件发送平台，请参考其文档设置发信域名的API USER及对应的KEY',
                this, {tips: 1}
            );
        });
        $('#tip-bulletin').on("mouseover click",function() {
            layer.tips('公告内容支持有限的HTML标签：<br>a、abbr、b、i、code、p、br、h3、h4、img',
                this, {tips: 1}
            );
        });
        $('#tip-uploadbeforehtml').on("mouseover click",function() {
            layer.tips('支持有限的HTML标签：<br>a、abbr、b、i、code、p、br、h3、h4',
                this, {tips: 1}
            );
        });
        $('#tip-register-options').on("mouseover click",function() {
            layer.tips('<b>开放注册：</b>允许任何人注册<br><b>注册审核：</b>限制新注册用户需要管理员审核后才能上传<br><b>禁止登录：</b>禁止普通用户登录（任何方式）',
                this, {tips: 1}
            );
        });
        $('#tip-proxy').on("mouseover click",function() {
            var tip = [
                '此代理用于程序请求外部HTTP[s]时(部分情况)，当首次请求超时、连接错误时自动调用代理。',
                '<b>格式：</b>http[s]=proxy-server,scheme://hostname=proxy-server',
                '详细请参考<a href="https://picbed.rtfd.vip/admin.html#picbed-admin-system" target="_blank" style="color:red">文档</a>！',
            ];
            layer.tips(tip.join("<br>"), this, {tips: 1});
        });
        //发送测试邮件
        $('#SendTestMail').on("click", function() {
            layer.prompt({
                formType: 0,
                title: '请输入测试邮箱',
                shade: 0
            }, function(value, index, elem){
                if (value) {
                    layer.close(index);
                    picbed.ajax("{{ url_for('api.test', Action='sendmail') }}", function(res) {
                        layer.msg("已发送测试邮件，请注意查收！", {icon:1});
                    }, {
                        data: {to:value}
                    });
                }
            });
        });
        //监听github扩展的jsdelivr域名选择框
        form.on('checkbox(jsdelivr)', function(data){
            if (data.elem.checked) {
                $("input[name='github_dn']").attr("disabled","disabled");
                $("input[name='github_dn']").addClass("layui-disabled");
            } else {
                $("input[name='github_dn']").removeAttr("disabled");
                $("input[name='github_dn']").removeClass("layui-disabled");
            }
        });
        //设置表单赋值（以下值存在html，为特殊情况）
        form.val("setting", {
            upload_beforehtml: picbed.escape2Html(`{{ g.site.upload_beforehtml }}`),
            bulletin: picbed.escape2Html(`{{ g.site.bulletin }}`),
            about: picbed.escape2Html(`{{ g.site.about }}`),
        });
        //提交系统设置
        form.on('submit(*)', function(data){
            if (document.forms['control'].checkValidity() === false) {
                layer.msg("发现部分参数不合法",{icon:0});
                document.forms['control'].reportValidity();
                return false;
            }
            var checkbox = data.form.querySelectorAll('input[type="checkbox"]');
            for (let i = 0; i < checkbox.length; ++i) {
                let item = checkbox[i];
                if (item.checked === true) {
                    data.field[item.name] = 1;
                } else {
                    data.field[item.name] = 0;
                }
            }
            if (data.field.cors && data.field.cors !== "*") {
                let cors = data.field.cors.split(/\s*,\s*/).join(",");
                if (cors.endsWith(",")) {
                    cors = cors.substring(0, cors.length-1);
                }
                data.field["cors"] = cors;
            }
            picbed.ajax("{{ url_for('api.config') }}", function(res) {
                if (res.code === 0) {
                    layer.msg("已修改配置", {icon:1});
                } else {
                    layer.msg(res.msg, {icon:0});
                }
            }, {
                data: data.field
            });
            return false;
        });
        //js监听键盘ctrl+s快捷键保存
        document.addEventListener('keydown', function(e){
            if ((location.hash === "#setting" || location.hash === "") && e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
                e.preventDefault();
                document.getElementById("saveSetting").click();
            }
        });
        //钩子扩展管理
        var tableIns = table.render({
            elem: "#hook",
            url: "{{ url_for('api.hook', Action='query') }}",
            method: "POST",
            cols: [
                [{
                        field: 'name',
                        title: '钩子名称',
                        sort: true,
                    },
                    {
                        field: 'version',
                        title: '版本',
                        width: 80
                    },
                    {
                        field: 'author',
                        title: '作者',
                        width: 100,
                        templet: function(d) {
                            return d.email ? ('<a href="mailto:' + d.email + '" title="Sendmail to author">' + d.author + '</a>') : d.author;
                        }
                    },
                    {
                        field: 'description',
                        title: '描述',
                    },
                    {
                        field: 'family',
                        title: '所属',
                        width: 70,
                        templet: function (d) {
                            return d.family === 'local' ? '<scan style="color:#5FB878">本地</scan>' : '<scan style="color:#FF5722">第三方</can>';
                        }
                    },
                    {
                        field: 'catalog',
                        title: '分类',
                        hide: true,
                        templet: function (d) {
                            return d.catalog ? d.catalog : '未分类';
                        }
                    },
                    {
                        field: 'mtime',
                        title: '修改时间',
                        hide: true,
                        templet: function (d) {
                            return util.toDateString(d.mtime * 1000);
                        }
                    },
                    {
                        field: 'ltime',
                        title: '加载时间',
                        hide: true,
                        templet: function (d) {
                            return util.toDateString(d.ltime * 1000);
                        }
                    },
                    {
                        field: 'state',
                        title: '状态',
                        width: 80,
                        sort: true,
                        templet: function (d) {
                            if (d.state === 'enabled') {
                                return '<scan style="color:#5FB878">启用中</scan>';
                            } else {
                                return '<scan style="color:#FF5722">已禁用</scan>';
                            }
                        }
                    },
                    {
                        fixed: 'right',
                        minWidth: 60,
                        align: 'left',
                        toolbar: '#hook_action'
                    }
                ]
            ],
            toolbar: '#toolbar',
            title: '钩子扩展列表',
            size: 'sm',
            skin: 'line',
            initSort: {
                field: 'state',
                type: 'desc'
            },
        });
        //监听表格工具条
        table.on('tool(hook)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'disable') { //禁用钩子
                picbed.ajax("{{ url_for('api.hook', Action='disable') }}", function(res) {
                    layer.msg("已禁用钩子：" + data.name, {icon:1});
                    obj.update({state:'disabled'});
                }, {
                    data: {name:data.name}
                });
            } else if (layEvent === 'enable') { //启用钩子
                picbed.ajax("{{ url_for('api.hook', Action='enable') }}", function(res) {
                    layer.msg("已启用钩子：" + data.name, {icon:1});
                    obj.update({state:'enabled'});
                }, {
                    data: {name:data.name}
                });
            } else if (layEvent === 'remove') { //删除第三方钩子
                layer.confirm('真的删除 ' + data.name + ' 么？', {icon:3,title:"请确认",shade:0.1}, function (index) {
                    layer.close(index);
                    //向服务端发送删除指令
                    picbed.ajax("{{ url_for('api.hook', Action='remove_third_hook') }}", function(res) {
                        layer.msg("已删除钩子：" + data.name, {icon:1});
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    }, {
                        data: {name: data.name}
                    });
                });
            }
        });
        //监听头部工具栏事件
        table.on('toolbar(hook)', function(obj){
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === "reloadHookManager") {
                picbed.ajax("{{ url_for('api.hook', Action='reload') }}", function(res) {
                    layer.msg("已重载钩子管理器", {icon:1});
                    tableIns.reload();
                });
            } else if (layEvent === "addHookName") {
                layer.prompt({
                    formType: 0,
                    title: '钩子模块名（本地需已安装）',
                    placeholder: '注意与钩子名称不同',
                    shade: 0.1
                }, function(value, index, elem){
                    if (value) {
                        layer.close(index);
                        picbed.ajax("{{ url_for('api.hook', Action='add_third_hook') }}", function(res) {
                            layer.msg("已添加钩子：" + value, {icon:1});
                            tableIns.reload();
                        }, {
                            data: {name:value}
                        });
                    }
                });
            } else if (layEvent === "installPackage") {
                laytpl(thirdhook_show_tpl.innerHTML).render({}, function(html){
                    layer.open({
                        type: 1,
                        title: '使用pip安装或升级第三方包',
                        closeBtn: false,
                        area: [
                            document.body.clientWidth > 580 ? '580px' : '95%',
                            '470px'
                        ],
                        shade: 0.1,
                        shadeClose: true,
                        maxmin: true,
                        content: html,
                        success: function(layero, indexo) {
                            var thidHooksTable = table.render({
                                elem: "#thid-hook-list",
                                url: "{{ url_for('api.github', Action='thirdHooks') }}",
                                cols: [[
                                    {
                                        field: 'name',
                                        title: '钩子名称',
                                        templet: "#thirhook_link",
                                        minWidth: 120
                                    },
                                    {
                                        field: 'module',
                                        title: '模块',
                                        hide: true,
                                    },
                                    {
                                        field: 'desc',
                                        title: '描述',
                                        minWidth: 100,
                                    },
                                    {
                                        field: 'author',
                                        title: '作者',
                                        width: 80,
                                        templet: function(d) {
                                            return `<a href="${d.home}" target="_blank" style="color:#01AAED">${d.author}</a>`;
                                        }
                                    },
                                    {
                                        field: 'catalog',
                                        title: '分类',
                                        hide: true,
                                    },
                                    {
                                        field: 'status_text',
                                        title: '状态',
                                        width: 80
                                    },
                                    {
                                        fixed: 'right',
                                        toolbar: '#thidhook_action',
                                        width: 45
                                    }
                                ]],
                                size: 'sm',
                                skin: 'line',
                                page: true,
                                limit: 5,
                                limits: [5, 10, 20],
                                width: document.body.clientWidth > 580 ? 560 : document.body.clientWidth * 0.9
                            });
                            $('#refreshThirdHookTable').click(function() {
                                thidHooksTable.reload({
                                    where: {no_fresh:0}
                                });
                            });
                            table.on('tool(thid-hook-list)', function(obj){
                                var data = obj.data;
                                var layEvent = obj.event;
                                var tr = obj.tr;
                                if(layEvent === 'install'){
                                    var pypiHide = data.pypi ? "" : "layui-hide";
                                    var content = [
                                        '<div style="padding: 15px">',
                                        `<h2 style="text-align:center">${data.name}</h2></br>`,
                                        '<div style="font-size:0.85em">',
                                        `<p>描述：${data.desc}</p>`,
                                        '<p>本地：' + (data.pkg.installed === true ? `已安装 ${data.pkg.local.version} 版本` : '未安装'),
                                        `<p>使用：请在安装成功后，在 <i>添加第三方钩子</i> 中输入模块名 <b style="color:#FF5722">${data.module}</b> 进行加载！</p>`,
                                        '</div></br><form class="layui-form" action="">',
                                        '从GitHub安装（留空使用master分支）<br>',
                                        '<input type="text" name="branch" placeholder="使用GitHub分支或标签" autocomplete="off" class="my-input">',
                                        `<input type="hidden" name="github" value="${data.github}">`,
                                        '<input type="hidden" name="index" value="">',
                                        '&nbsp;<button type="button" title="提交安装" class="layui-btn layui-btn-xs layui-btn-radius" lay-submit lay-filter="install-from-github"><i class="layui-icon layui-icon-ok"></i></button>',
                                        `</form><br><form class="layui-form ${pypiHide}" action="">`,
                                        '从Pypi安装<br>',
                                        '<input type="text" name="version" placeholder="[可选]使用Pypi版本号" autocomplete="off" class="my-input">',
                                        `<input type="hidden" name="pkg" value="${data.pkg.name}">`,
                                        '<input type="hidden" name="index" value="">',
                                        '&nbsp;<button type="button" title="提交安装" class="layui-btn layui-btn-xs layui-btn-radius" lay-submit lay-filter="install-from-pypi"><i class="layui-icon layui-icon-ok"></i></button>',
                                        '</form></div>'
                                    ].join("");
                                    layer.open({
                                        type: 1,
                                        title: false,
                                        closeBtn: true,
                                        area: '300px',
                                        anim: 2,
                                        shade: 0.1,
                                        shadeClose: true,
                                        content: content,
                                        success: function(layero, index) {
                                            var i = layero[0].querySelectorAll("input[name='index']");
                                            i[0].value = index;
                                            i[1].value = index;
                                        }
                                    });
                                }
                            });
                        }
                    });
                });
            }
        });
        //监听安装
        form.on('submit(install-from-github)', function(data){
            var branch = data.field.branch || 'master',
                value = `git+${data.field.github}@${branch}`;
            picbed.ajax("{{ url_for('api.pip_install') }}", function(res) {
                layer.msg(`正在安装${value}，请稍后...`, {time:1000}, function() {
                    if (data.field.index) layer.close(data.field.index);
                });
            }, {
                data: {package:value, upgrade:true}
            });
            return false;
        });
        form.on('submit(install-from-pypi)', function(data){
            var pkg = data.field.pkg,
                value = data.field.version ? `${pkg}==${data.field.version}` : pkg;
            picbed.ajax("{{ url_for('api.pip_install') }}", function(res) {
                layer.msg(`正在安装${value}，请稍后...`, {time:1000}, function() {
                    if (data.field.index) layer.close(data.field.index);
                });
            }, {
                data: {package:value, upgrade:true}
            });
            return false;
        });
        //检测版本更新
        picbed.ajax("{{ url_for('api.github', Action='latestRelease') }}", function(res) {
            if (res.show_upgrade === true) {
                $("#verTip").html("<a href='" + res.data.html_url + "' style='color:#FF5722' target='_blank'>新版本</a>");
                layer.tips("发布于" + picbed.convertUTCTimeToLocalTime(res.data.published_at), "#verTip", {tips:3});
            }
        }, {
            method: 'get'
        });
        //用户管理
        table.render({
            elem: "#usermanager",
            url: "{{ url_for('api.user') }}",
            method: "GET",
            cols: [
                [
                    {
                        field: 'username',
                        title: '用户名',
                        minWidth: 100,
                        templet: function(d) {
                            var login_tip = d.login_at ? (
                                '上次登录于 ' + util.toDateString(d.login_at * 1000) + (d.login_ip ? ` @ ${d.login_ip || ''}` : '')
                            ) : '';
                            var user = d.avatar ? (`<a href="${d.avatar}" class="layui-table-link" target="_blank">${d.username}</a>`) : d.username;
                            user = `<scan title="${login_tip}">${user}</scan>`;
                            return d.pics > 0 ? (user + '&nbsp;<span class="layui-badge layui-bg-black">' + d.pics + '</span>') : user;
                        }
                    },
                    {
                        field: 'nickname',
                        title: '昵称',
                    },
                    {
                        field: 'email',
                        title: '邮件',
                        templet: function (d) {
                            return d.email ? (d.email_verified === 1 ? ('<scan style="color:#009688">' + d.email + '</scan>') : d.email) : "";
                        }
                    },
                    {
                        field: 'label',
                        title: '标签',
                        edit: 'text',
                        width: 90,
                        templet: function(d) {
                            return d.label.join(",");
                        }
                    },
                    {
                        field: 'is_admin',
                        title: '管理员',
                        width: 70,
                        align: "center",
                        templet: '#is_admin_tpl'
                    },
                    {
                        field: 'ctime',
                        title: '注册时间',
                        width: 150,
                        templet: function (d) {
                            var ctime = util.toDateString(d.ctime * 1000),
                                mtime = util.toDateString(d.mtime * 1000);
                            return d.mtime ? ('<scan title="资料修改于' + mtime + '" style="border-bottom:1px dashed #0081EF">' + ctime + '</scan>') : ctime;
                        }
                    },
                    {
                        field: 'status',
                        title: '状态',
                        width: 80,
                        templet: function (d) {
                            if (d.status === 0) {
                                return '<scan style="color:#FF5722">已禁用</scan>';
                            } else if (d.status === -1) {
                                return '<scan style="color:#01AAED;border-bottom:1px dashed #0081EF" title="' + (d.message||"(无)") + '">待审核</scan>';
                            } else if (d.status === -2) {
                                return '<scan style="color:red;border-bottom:1px dashed #0081EF" title="' + (d.status_reason||"(无)") + '">审核拒绝</scan>';
                            } else {
                                return '<scan style="color:#5FB878">启用中</scan>';
                            }
                        }
                    },
                    {
                        fixed: 'right',
                        align: 'left',
                        minWidth: 70,
                        toolbar: '#usermanager_action'
                    }
                ]
            ],
            //toolbar: '#toolbar',
            //title: '用户列表',
            size: 'sm',
            skin: 'line',
            page: true,
        });
        //监听设置管理员操作
        form.on('checkbox(set_admin)', function (obj) {
            var username = this.dataset.username,
                v = obj.elem.checked ? 1 : 0;
            picbed.ajax("{{ url_for('api.user', Action='admin') }}", function(res) {
                layer.msg(v === 1 ? "已设置为管理员" : "已取消管理员", {icon:1});
            }, {
                method: "PUT",
                data: {username: username, is_admin: v}
            })
        });
        //用户管理监听表格工具条
        table.on('tool(usermanager)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'remove') { //删除第三方钩子
                layer.confirm('真的删除用户 ' + data.username + ' 么？<br><b style="color:red">此操作将会清除用户数据且不可逆转！</b>', {icon:3,title:"请确认",shade:0.1}, function (index) {
                    layer.close(index);
                    //向服务端发送删除指令
                    picbed.ajax("{{ url_for('api.user') }}", function(res) {
                        layer.msg("已删除用户", {icon:1});
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    }, {
                        method: "DELETE",
                        data: {username: data.username}
                    });
                });
            } else if (layEvent === "review") {
                let tip = '点击通过会直接激活 ' + data.username + ' 用户哦！<br>如果不想，可以拒绝或关闭哦。<br><br>如已设置注册审核邮箱且用户验证了邮箱，那么无论通过还是拒绝，均为向用户发送通知！';
                layer.confirm(tip,{icon:0,title:"请确认",btn:["通过","拒绝"],shade:0.1}, function (index) {
                    picbed.ajax("{{ url_for('api.user', Action='reviewOK') }}", function(res) {
                        layer.msg("已审核通过，允许用户上传图片！", {icon:1});
                        obj.update({status:1});
                    }, {
                        method: "PUT",
                        data: {username: data.username}
                    });
                }, function (index) {
                    layer.close(index);
                    layer.prompt({
                        formType: 0,
                        value: '',
                        title: '请输入拒绝理由哦',
                        shade: 0.1,
                    }, function(value, index, elem){
                        picbed.ajax("{{ url_for('api.user', Action='reviewFail') }}", function(res) {
                            layer.close(index);
                            layer.msg("审核拒绝", {icon:1});
                            obj.update({status:-2});
                        }, {
                            method: "PUT",
                            data: {username: data.username, reason: value}
                        });
                    });
                });
            } else if (layEvent === "disable") {
                picbed.ajax("{{ url_for('api.user', Action='disable') }}", function(res) {
                    layer.msg("已禁用！", {icon:1});
                    obj.update({status:0});
                }, {
                    method: "PUT",
                    data: {username: data.username}
                });
            } else if (layEvent === "enable") {
                picbed.ajax("{{ url_for('api.user', Action='enable') }}", function(res) {
                    layer.msg("已启用！", {icon:1});
                    obj.update({status:1});
                }, {
                    method: "PUT",
                    data: {username: data.username}
                });
            }
        });
        //用户管理监听单元格编辑
        table.on('edit(usermanager)', function(obj) {
            picbed.ajax("{{ url_for('api.user', Action='label') }}", function (res) {
                var tip = obj.value ? ("已设置用户标签为：" + obj.value) : "已取消标签";
                layer.msg(tip, {icon:1});
            }, {
                method: "PUT",
                data: {username: obj.data.username, label: obj.value}
            })
        });
    });
</script>
{{ intpl("adminscript") }}
{% endblock %}
